<?php

namespace App\Lib;

use App\Models\Traits\Curl;

class Huomao implements RoomAnalyze
{
    use Curl;

    public function getRoomInfo(string $url): array
    {
        return $this->analyzeResponse($this->curlGet($url));
    }

    protected function analyzeResponse(string $response): array
    {
        preg_match("/var channelOneInfo = (.*);\r\n/", $response, $preg_res);
        if (isset($preg_res[1])) {
            $room_info = json_decode($preg_res[1], true);
        }
        return isset($room_info) ? $this->formatData($room_info) : [];
    }

    protected function formatData(array $roomInfo): array
    {
        return [
            'id'    => $roomInfo['room_number']??'',
            'name'  => $roomInfo['channel']??'',
            'owner' => $roomInfo['username']??'',
            'pop'   => $roomInfo['views']??'',
            'src'   => $roomInfo['image']??''
        ];
    }
}